package com.itany.mall.dao.impl;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.itany.mall.dao.OrderDetailDao;
import com.itany.mall.entity.OrderDetail;
import com.itany.mall.exception.DataAccessException;
import com.itany.mall.factory.Component;

import database.Database02;

@Component
public class OrderDetailDaoImpl implements OrderDetailDao {

	@Override
	public void saveOrUpdateOrderDetails(Collection<OrderDetail> values) {
		// 什么样的数据save?
		// 什么样的数据update?
		// 如果是已保存过的od对象, 有id
		// 如果是新的od对象, 没有id
		for (OrderDetail od : values) {
			if (od.getId() == null) {
				save(od);
				continue;
			}
			update(od);
		}
	}

	private void update(OrderDetail od) {
		try {
			File f = new File(Database02.ORDER_DETAIL_DIR,
					od.getId() + Database02.ORDER_DETAIL_SUFFIX);

			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
			OrderDetail od0 = (OrderDetail) ois.readObject();
			ois.close();

			od0.setProductSum(od.getProductSum());
			od0.setPrice(od.getPrice());

			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
			oos.writeObject(od0);
			oos.flush();
			oos.close();

		} catch (Exception e) {
			e.printStackTrace();
			throw new DataAccessException(e);
		}
	}

	private void save(OrderDetail od) {
		try {
			od.setId(Database02.autoIncrement(Database02.ORDER_DETAIL_DIR));
			File f = new File(Database02.ORDER_DETAIL_DIR,
					od.getId() + Database02.ORDER_DETAIL_SUFFIX);
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
			oos.writeObject(od);
			oos.flush();
			oos.close();
		} catch (IOException e) {
			e.printStackTrace();
			throw new DataAccessException(e);
		}
	}

	@Override
	public List<OrderDetail> selectByUserId(long userId) {
		List<OrderDetail> list = new ArrayList<>();
		File dir = new File(Database02.ORDER_DETAIL_DIR);
		File[] fs = dir.listFiles(new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				return pathname.getName().endsWith(Database02.ORDER_DETAIL_SUFFIX);
			}
		});

		try {
			for (File f : fs) {
				ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
				OrderDetail od = (OrderDetail) ois.readObject();
				ois.close();
				if (od.getUser().getId().equals(userId)) {
					list.add(od);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataAccessException(e);
		}

		return list;
	}

	@Override
	public List<OrderDetail> selectByOrderNum(String num) {
		List<OrderDetail> list = new ArrayList<>();
		File dir = new File(Database02.ORDER_DETAIL_DIR);
		File[] fs = dir.listFiles(new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				return pathname.getName().endsWith(Database02.ORDER_DETAIL_SUFFIX);
			}
		});

		try {
			for (File f : fs) {
				ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
				OrderDetail od = (OrderDetail) ois.readObject();
				ois.close();
				if (od.getOrder().getOrderNum().equals(num)) {
					list.add(od);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataAccessException(e);
		}

		return list;
	}

}
